color temporal = color(20, 0, 0);
color permanent = color(0, 20, 0);
color backgroundColor = color(255, 255, 255);

int w = 800;
int h = 600;
int windowDim = 100;
int x0, y0;
int numFigures = 10;
boolean foundButton=false;
Button buttonPressed = null;
Button buttonUnderMouse = null;
ArrayList<Canvas> canvas = new ArrayList<Canvas>();
Canvas cb;
int WINDOW_SIZE = w/5;

private final int BUTTON_SIZE = 40;

ArrayList<Button> buttons = new ArrayList<Button>();
StickMan stickMan = new StickMan();
PGraphics pg;
Node trunkWorld;
//Draw Stickman
void setup() {

  size(w, h);
  background(backgroundColor);

  buttons = new ArrayList<Button>();
  
  //Draw window view
  Polygon window = new Polygon();
  window.addPoint(0,0);
  window.addPoint(0,WINDOW_SIZE);
  window.addPoint(WINDOW_SIZE,WINDOW_SIZE);
  window.addPoint(WINDOW_SIZE,0);
  
  
  //Window action buttons
  PImage iconMoveWindow = loadImage("icons/dotted square.png");
  PImage iconRotateWindow = loadImage("icons/rotate.png");
  PImage iconScaleWindow = loadImage("icons/scale.png");
  Action move = new TranslatePolygon(window);
  buttons.add(new Button(iconMoveWindow, iconMoveWindow, 280, 0, BUTTON_SIZE, move));
  Action rotate = new RotatePolygon(window);
  buttons.add(new Button(iconRotateWindow, iconRotateWindow, 320, 0, BUTTON_SIZE, rotate));
  Action scale = new ScalePolygon(window);
  buttons.add(new Button(iconScaleWindow,iconScaleWindow, 360, 0, BUTTON_SIZE, scale));
  
  
  
  //Draw Stickman
  Node stickManNode = stickMan.create(buttons,BUTTON_SIZE);
  
  //Create two canvas
  canvas.add(new CanvasTop(new PVector(0,BUTTON_SIZE),(h-BUTTON_SIZE)/2,w,backgroundColor,stickManNode,window));
  cb = new CanvasBottom(new PVector(0,(h+BUTTON_SIZE)/2),(h-BUTTON_SIZE)/2,w,backgroundColor,stickManNode,window);
  canvas.add(cb);
  
  
  
}

void draw() {
  for(Canvas c : canvas){
    c.draw();
  }
}



void mouseMoved() {
  foundButton = false;
  for (Button button :  buttons) {
    if (button.isInside(mouseX, mouseY)) {
      foundButton = true;
      buttonUnderMouse = button;
      button.mouseOverBotton();
    }
    else {
      button.drawButton(false);
    }
  } 
  if (!foundButton) {
    buttonUnderMouse = null;
  }
  for(Canvas c : canvas){
    c.mouseMove(mouseX,mouseY);
  }
}

void mouseClicked(){
  if(buttonUnderMouse!=null) buttonPressed = buttonUnderMouse;
  for(Canvas c : canvas){
    c.mouseClick(mouseX,mouseY);
  }
}

void mousePressed() {
  if (buttonPressed != null) {
    cb.setActiveAction(buttonPressed.getAction());
  }
  for(Canvas c : canvas){
    c.mousePress(mouseX,mouseY);
  }
}

void mouseDragged() {
  for(Canvas c : canvas){
    c.mouseDrag(mouseX,mouseY);
  }
}






/*
ArrayList<Polygon> figures = new ArrayList<Polygon>();
 Polygon window = new Polygon();
 Polygon windowDrawn;
 ArrayList<Polygon> windowView = new ArrayList<Polygon>();
 
 void setup() {
 size(w, h);
 background(backgroundColor);
 
 window.addPoint(0, 0);
 window.addPoint(0, windowDim);
 window.addPoint(windowDim, windowDim);
 window.addPoint(windowDim, 0);
 
 
 for (int i=0; i < numFigures; i++) {
 Polygon p = new Polygon();
 int size = 500;
 int x = (int)random(w-size);
 int y = (int)random(h-size);
 int pSize = (int)random(size);
 for (int j=0; j < (int)random(10); j++) {
 p.addPoint(new PVector(x+random(pSize), y+random(pSize)));
 }
 figures.add(p);
 }
 
 
 windowDrawn = window;
 }
 
 void draw() {
 background(backgroundColor);
 windowDrawn.draw();
 
 for (int i=0; i < figures.size(); i++) {
 figures.get(i).draw();
 } 
 for (int i=0; i < windowView.size(); i++) {
 windowView.get(i).setFgColor(color(255, 204, 0));
 windowView.get(i).draw();
 }
 ArrayList<PVector> n = windowDrawn.getNormals();
 for (int i=0; i < n.size(); i++) {
 Polygon tmp = new Polygon(color(255, 204, 0));
 tmp.addPoint(windowDrawn.getPoint(i));
 PVector t = PVector.add(windowDrawn.getPoint(i), PVector.mult(n.get(i), 10.0f));
 tmp.addPoint(t);
 tmp.draw();
 }
 }
 
 void mousePressed() {
 x0 = mouseX;
 y0 = mouseY;
 }
 
 void mouseDragged() {
 windowDrawn = transform(window, Matrix4.translation(mouseX-x0, mouseY-y0));
 }
 
 void mouseReleased() {
 window.translate(mouseX-x0, mouseY-y0);
 windowView = new ArrayList<Polygon>();
 for (int i=0; i < figures.size(); i++) {
 ArrayList<Polygon> cut = figures.get(i).cut(window);
 windowView.addAll(cut);
 }
 windowDrawn = window;
 }
 
 
 Polygon transform(Polygon original, Matrix4 transformation) {
 Polygon newPolygon = original.clone();
 newPolygon.transform(transformation);
 return newPolygon;
 }
 
 
 */
